﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

public partial class Control_Menu : System.Web.UI.UserControl
{
    
    public List<UrlItem> Menu
    {
        set
        {
            ViewState["Menu"] = value;
 
        }
        get
        {
            object o = ViewState["Menu"];
            return (o == null) ? new List<UrlItem>() : (List<UrlItem>)o; 
 
        }
    
    }
     public List<UrlItem> SubMenu
    {
        set
        {
            ViewState["SubMenu"] = value;
 
        }
    
    }
     public Dictionary<string, UrlItem> Menus
    {
        //get { return options; }
        set
        {
            List<UrlItem> list = new List<UrlItem>();
            foreach(KeyValuePair<string,UrlItem> key in value)
            {
                list.Add(key.Value);
            }
            rp_Menu.DataSource = list;
            rp_Menu.DataBind();
           

        }
    }
     public Dictionary<string, UrlItem> SubMenus
     {
         //get { return options; }
         set
         {
             List<UrlItem> list = new List<UrlItem>();
             foreach (KeyValuePair<string, UrlItem> key in value)
             {
                 list.Add(key.Value);
             }
             rp_SubMenu.DataSource = list;
             rp_SubMenu.DataBind();


         }
     }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!PageRequest().Equals("Warring"))
        MenuBind();
      
       
    }
    private void MenuBind()
    {
       
        Dictionary<string, UrlItem> mainMenu = new Dictionary<string, UrlItem>();
        Dictionary<string, UrlItem> subMenus = new Dictionary<string, UrlItem>();
  
        Dictionary<string, UrlItem> menus = this.ReadMenuData();
        string page = PageRequest();


        foreach (KeyValuePair<string, UrlItem> key in menus)
        {
            if (key.Value.ParentId.Equals("0"))
                mainMenu.Add(key.Value.Id, key.Value);
            else
                subMenus.Add(key.Value.Id, key.Value);
        }  

        //menus[page].Current = "id='current'";

        if (menus[page].ParentId.Equals("0"))
        {

            //Bind Main Menu
            MenuDataBind(mainMenu, page);

            SubMenuDataBind(subMenus, page, menus[page].Id);

        }
        else
        {
            MenuDataBind(mainMenu, menus[page].ParentId);
            SubMenuDataBind(subMenus, page, menus[page].ParentId);


        }

       
 
    }
    private void MenuDataBind(Dictionary<string, UrlItem> urls, string page)
    {
        if (urls[page] != null)
            urls[page].Current = "id='current'";

        Menus = urls;

    }

    private void SubMenuDataBind(Dictionary<string, UrlItem> subMenus, string page,string parentId)
    {
           
        Dictionary<string, UrlItem> menus = new Dictionary<string,UrlItem>();
    

        //if (subMenus[page] != null)
        //    subMenus[page].Current = "id='current'";
        foreach (KeyValuePair<string, UrlItem> key in subMenus)
        {
            if (key.Value.ParentId.Equals(parentId))
            {
                menus.Add(key.Value.Id, key.Value);
               
            }
            if (key.Value.Id.Equals(page))
                subMenus[page].Current = "id='current'";
            
        }

        SubMenus = menus;
    }

    public Dictionary<string,UrlItem> CreateMenu()
    {
         Dictionary<string, UrlItem> urls = new Dictionary<string, UrlItem>();
  
        UrlItem url = new UrlItem();
        url.Id = "Home";
        url.Text = "Home";
        url.Url = "#";
        urls.Add(url.Id,url);
        ////
        url = new UrlItem();
        url.Id = PageView.ProjectImage;
        url.Text = "Project Image";
        url.Url = DoAction.ActionPageView(PageView.ProjectImage, Action.LIST);
        urls.Add(url.Id, url);
        ///
        url = new UrlItem();
        url.Id = PageView.ModuleManager;
        url.Text = "Module Manager";

        url.Url = DoAction.ActionPageView(PageView.ModuleManager, Action.LIST);
        urls.Add(url.Id, url);

        ////
        url = new UrlItem();
        url.Id = PageView.AddinManager;
        url.Text = "Addin Manager";

        url.Url = DoAction.ActionPageView(PageView.AddinManager, Action.LIST);
        urls.Add(url.Id, url);

        return urls;
    }
    public Dictionary<string,UrlItem> CreateSubMenu()
    {
      
       
        return null;

    }
    /// <summary>
    /// Read menu data
    /// </summary>
    /// <returns></returns>
    public Dictionary<string, UrlItem> ReadMenuData()
    {
        Dictionary<string, UrlItem> menus = new Dictionary<string, UrlItem>();
        
        DataSet ds = new DataSet();
        string filePath = HttpContext.Current.Server.MapPath("~/App_Data/Configures/");
        string fileName = "Menu.xml";
        ds.ReadXml(filePath + fileName);

        foreach (DataTable table in ds.Tables)
        {
            foreach (DataRow row in table.Rows)
            {
                //DataRow row = table.Rows[0];
                UrlItem url = new UrlItem();
                url.ParentId = row["parentId"].ToString();
                url.Id = row["id"].ToString();
                url.Text = row["text"].ToString();
                url.Url = DoAction.ActionPageView(row["page"].ToString(), row["action"].ToString());

                menus.Add(url.Id, url);
            }
        }
        return menus;
    }
    private string PageRequest()
    {
        string page = "Home";
        object o = Request.QueryString["page"];
        if (o != null)
        {
            page = o.ToString();
        }
        return page;

    }


}
